package com.example.administrator.myapp;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.listener.MyListener;
import com.example.administrator.model.Question;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/20 0020.
 */

public class IndexActivity extends AppCompatActivity
{

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private final static String TAG = "IndexActivity";
    private final static String INDEX_KEY = "index";
    private Button mNextButton;
    private Button mTrueButton;
    private Button mFalseButton;
    private Button mPrevious;

    public Button getCheatButton() {
        return mCheatButton;
    }

    public void setCheatButton(Button cheatButton) {
        mCheatButton = cheatButton;
    }

    private Button mCheatButton;

    public Button getPrevious() {
        return mPrevious;
    }

    public void setPrevious(int previous)
    {
       this.mPrevious = (Button)this.findViewById(previous);
    }

    private Question mQuestion;
    private Question[] mQuestionAnwer = new Question[]{new Question(R.string.math, false), new Question(R.string.question, false)};;
    private TextView mTextView;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient mClient;

    public int getResultId() {
        return resultId;
    }

    public void setResultId(int resultId) {
        this.resultId = resultId;
    }

    private int resultId ;

    public Question[] getQuestionAnwer() {
        return mQuestionAnwer;
    }

    public void setQuestionAnwer(Question[] questionAnwer) {
        mQuestionAnwer = questionAnwer;
    }

    public Question getQuestion() {
        return mQuestion;
    }

    public void setQuestion(Question question) {
        mQuestion = question;
    }

    public Button getNextButton() {
        return mNextButton;
    }

    public void setNextButton(Button nextButton) {
        mNextButton = nextButton;
    }

    public Button getTrueButton() {
        return mTrueButton;
    }

    public void setTrueButton(Button trueButton) {
        mTrueButton = trueButton;
    }

    public Button getFalseButton() {
        return mFalseButton;
    }

    public void setFalseButton(Button falseButton) {
        mFalseButton = falseButton;
    }
    private int mCurretIndex = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null)
        {
            this.mCurretIndex = savedInstanceState.getInt(INDEX_KEY);
        }
        this.setContentView(R.layout.activity_index);

        Log.d(TAG, "onCreate() called");

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        this.mTextView = (TextView)this.findViewById(R.id.questions);
        this.mTextView.setText(mQuestionAnwer[mCurretIndex].getTextResId());

        this.setFalseButton((Button)this.findViewById(R.id.InCorrect));
        this.getFalseButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IndexActivity.this.checkAnwer(false);
            }
        });

        this.setTrueButton((Button)this.findViewById(R.id.Correct));
        this.getTrueButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IndexActivity.this.checkAnwer(true);
            }
        });
        this.setNextButton((Button)this.findViewById(R.id.nextButton));

        this.getNextButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              IndexActivity.this.setTxtListener();
            }
        });
        this.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IndexActivity.this.setTxtListener();
            }
        });

//        this.setPrevious(R.id.previous);
//        this.getPrevious().setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                IndexActivity.this.setTxtListener(2);
//            }
//        });
        this.setCheatButton((Button)this.findViewById(R.id.Cheat));

        this.getCheatButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyListener listener = new MyListener();

                listener.getHomeActivity(ShowAnwerActivity.class, IndexActivity.this, 6);
            }
        });

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    protected void setTxtListener()
    {
        int index = this.mCurretIndex;
        this.mCurretIndex = (index+1) % this.mQuestionAnwer.length;
        this.setNextQuestion();
    }


    /**
     * 保存数据屏幕旋转时
     */
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);
        Log.i(TAG, "onSaveIntanceState");
        savedInstanceState.putInt(INDEX_KEY, this.mCurretIndex);
    }

    protected void setTxtListener(int a)
    {
        int index = this.mCurretIndex;
        if (index <= 0)
        {
            this.mCurretIndex = 0;
            this.setNextQuestion();
        }
        else
        {
            this.mCurretIndex = (index-1) % this.mQuestionAnwer.length;

            this.setNextQuestion();
        }

    }
    /**
     * 更新问题
     */
    protected void setNextQuestion()
    {
        int next = mQuestionAnwer[this.mCurretIndex].getTextResId();

        this.mTextView.setText(next);
    }
    protected List<Map<Integer, Boolean>> getQuestions() {

        List<Map<Integer, Boolean>> listData = new ArrayList<>();


        Map<Integer, Boolean> map = new HashMap<>();

        map.put(R.string.question, false);
        map.put(R.string.math, false);

        listData.add(0, map);

        return listData;
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("Index Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart called");
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        mClient.connect();
        AppIndex.AppIndexApi.start(mClient, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d(TAG, "onStop called");
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        AppIndex.AppIndexApi.end(mClient, getIndexApiAction());
        mClient.disconnect();
    }


    /**
     * 检测是用户点击的按钮
     * @param  isTrue 按钮
     */
    protected void checkAnwer(boolean isTrue)
    {
        boolean userPressed = mQuestionAnwer[this.mCurretIndex].isAnwerTrue();

        int messageId = 0;

        if (isTrue == userPressed)
        {
            messageId = R.string.answerTrue;
        }
        else
        {
            messageId = R.string.answerFalse;
        }

        Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
    }

    protected Button getButton(int resultId)
    {
        this.setResultId(resultId);
        return (Button)this.findViewById(resultId);
    }


}
